/**
* 版权声明：厦门睿商网络科技有限公司 版权所有 违者必究
* 日    期：2020-04-20
*/
package com.rzico.core.service;

import com.rzico.base.BaseMapper;
import com.rzico.base.impl.BaseServiceImpl;
import com.rzico.core.entity.SysMenu;
import com.rzico.core.entity.SysOffice;
import com.rzico.core.entity.SysReportCategory;
import com.rzico.util.StringUtils;
import com.rzico.util.TreeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.rzico.core.mapper.SysReportCategoryMapper;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * <pre>
 * 统计报表业务类
 * </pre>
 *
 * @author Rzico Boot
 * @version 1.0
 */
@Service
public class SysReportCategoryService extends BaseServiceImpl<SysReportCategory, String> {

    @Autowired
    private SysReportCategoryMapper sysReportCategoryMapper;

    @Autowired
    private TreeUtils<SysReportCategory> treeUtils;

    @Override
    public BaseMapper<SysReportCategory, String> getMapper() {
        return sysReportCategoryMapper;
    }

    /**
     * 排序
     *
     * @param items
     *            排序
     * @return 排序
     */
    private List<SysReportCategory> sort(List<SysReportCategory> items) {
        List<SysReportCategory> result = new ArrayList<SysReportCategory>();
        if (items != null) {
            Collections.sort(items, new Comparator<SysReportCategory>() {
                public int compare(SysReportCategory n1, SysReportCategory n2) {
                    if (n1.getOrders()==null) {
                        return -1;
                    }
                    if (n2.getOrders()==null) {
                        return 1;
                    }
                    return n1.getOrders().compareTo(n2.getOrders());
                }
            });
            for (SysReportCategory item : items) {
                result.add(item);
                item.setChildrens(sort(item.getChildrens()));

            }
        }
        return result;
    }

    public List selectTree(Map<String, Object> params) throws Exception{
        List<SysReportCategory> list = super.selectByExample(SysReportCategory.class,params);
        return sort(treeUtils.buildTree(list));
    }

    /**
     * 生成treePath
     * @param sysReportCategory
     */
    public void setTreePath(SysReportCategory sysReportCategory){
        SysReportCategory parent = this.selectByPrimaryKey(sysReportCategory.getParentId());
        if (null != parent){
            sysReportCategory.setTreePath(parent.getTreePath() + sysReportCategory.getId() + SysReportCategory.TREE_PATH_SEPARATOR);
        }else{
            sysReportCategory.setTreePath(SysReportCategory.TREE_PATH_SEPARATOR + sysReportCategory.getId() + SysReportCategory.TREE_PATH_SEPARATOR);
        }
    }

    /**
     * 查找当前商户下当前节点的所有子节点
     * @param sysReportCategory
     * @return
     */
    public List<SysReportCategory> selectAllChildren(SysReportCategory sysReportCategory){
        return sysReportCategoryMapper.selectAllChildren(sysReportCategory);
    }

    @Override
    public int insert(SysReportCategory sysReportCategory) {
        if (StringUtils.isNotEmpty(sysReportCategory.getParentId())) {
            setTreePath(sysReportCategory);
        }else{
            //根节点的treepath是自己的id
            sysReportCategory.setTreePath(SysReportCategory.TREE_PATH_SEPARATOR + sysReportCategory.getId() + SysReportCategory.TREE_PATH_SEPARATOR);
        }
        return super.insert(sysReportCategory);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateByPrimaryKeySelective(SysReportCategory record) {
        //todo 查找当前节点的treepath
        if (null != record.getParentId()){
            setTreePath(record);
        }
        int num = super.updateByPrimaryKeySelective(record);
        if (num > 0){
            //更新子节点的treePath
            List<SysReportCategory> list = selectAllChildren(record);
            if (null != list && list.size() >0){
                for (SysReportCategory sysOffice : list){
                    setTreePath(sysOffice);
                    super.updateByPrimaryKeySelective(sysOffice);
                }
            }
        }
        return num;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateByPrimaryKey(SysReportCategory record) {
        //todo 查找当前节点的treepath
        if (null != record.getParentId()){
            setTreePath(record);
        }
        int num = super.updateByPrimaryKey(record);
        if (num > 0){
            //更新子节点的treePath
            List<SysReportCategory> list = selectAllChildren(record);
            if (null != list && list.size() > 0){
                for (SysReportCategory sysOffice : list){
                    setTreePath(sysOffice);
                    super.updateByPrimaryKey(sysOffice);
                }
            }
        }
        return num;
    }

}
